import {login, logout, getInfo, getQrCode, getQrCodeCheck,faceLogin,getCheckFace} from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'

const state = {
  token: getToken(),
  name: '',
  avatar: '',
  roles: [],
  permissions: []
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_PERMISSIONS: (state, permissions) => {
    state.permissions = permissions
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  }
}

const actions = {
  setMyToken({ commit },token){
    commit('SET_TOKEN', token)
    setToken(token)
  },

  // 用户登录
  login({ commit }, userInfo) {
    // 从userInfo里面取出username，password
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: password }).then(response => {
        const { data } = response
        commit('SET_TOKEN', data.token)
        setToken(data.token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 获取用户信息
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const { data } = response
        const { roles, name, picture, permissions } = data

        if (!name) {
          reject('未登录，请先登录')
        }

        commit('SET_ROLES', roles) // 角色
        commit('SET_NAME', name) // 用户名
        commit('SET_AVATAR', picture) // 头像
        commit('SET_PERMISSIONS', permissions) // 头像
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 退出
  logout({ commit, state, dispatch }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        commit('SET_TOKEN', '')
        commit('SET_ROLES', [])
        commit('SET_PERMISSIONS', [])
        removeToken()
        resetRouter() // 重置路由

        dispatch('tagsView/delAllViews', null, { root: true })

        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '')
      commit('SET_ROLES', [])
      commit('SET_PERMISSIONS', [])
      removeToken()
      resolve()
    })
  },

  // 获取二维码
  getQrCode({ commit }) {
    // 从userInfo里面取出username，password
    return new Promise((resolve, reject) => {
      getQrCode().then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 查询二维码状态
  qrCodeCheck({ commit },code) {
    // 从userInfo里面取出username，password
    return new Promise((resolve, reject) => {
      getQrCodeCheck(code).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 登录
  faceLogin({ commit },data9) {
    // 从userInfo里面取出username，password
    return new Promise((resolve, reject) => {
      faceLogin(data9).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 检测人脸
  checkFace({ commit },formData) {
    // 从userInfo里面取出username，password
    return new Promise((resolve, reject) => {
      getCheckFace(formData).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },

}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
